<?php
/**
 * Created by PhpStorm.
 * User: sin
 * Date: 2019/2/22
 * Time: 10:59 AM
 */

namespace app\common\ext;

use app\common\cache\agent\IAgentAdminCache;
use app\common\enum\IsReal;
use app\common\enum\PoolBidStatus;
use app\common\enum\TenderStatus;

class UserPoolBid extends ICommon{

    /**
     * 开标
     * @param $cooperate_admin_id
     * @param $bid_id
     * @param $agent_admin_tender_id
     * @return bool
     */
    public static function open($cooperate_admin_id, $bid_id, $agent_admin_tender_id){
        $where['user_pool_bid_id'] = $bid_id;
        $where['cooperate_admin_id'] = $cooperate_admin_id;
        $bid_info = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->row();
        if(empty($bid_info)){
            self::$error = "订单不存在！";
            return false;
        }

        if(!in_array($bid_info['pool_bid_status'],[PoolBidStatus::BidDown,PoolBidStatus::Bid])){
            self::$error = "订单状态错误！";
            return false;
        }

        $where_tender['agent_admin_tender_id'] = $agent_admin_tender_id;
        $tender_info = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($where_tender)
            ->row();
        if(!$tender_info){
            self::$error = "抢单信息不存在！";
            return false;
        }

        IDb::dbStartTrans();

        $where['user_pool_bid_id'] = $bid_id;

        $data['pool_bid_status'] = PoolBidStatus::BidOpen;
        $data['agent_id'] = $tender_info['agent_id'];
        $data['agent_admin_id'] = $tender_info['agent_admin_id'];
        $data['open_time'] = get_date_time();
        $result = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->setDbData($data)
            ->upd();
        if(!$result){
            IDb::dbRollback();
            self::$error = '确认失败';
            return false;
        }

        //先把所有投标改成未中标
        $where_not_bid['user_pool_bid_id'] = $bid_id;
        $data_not_bid['tender_status'] = TenderStatus::NotBid;
        $result_not_bid = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($where_not_bid)
            ->setDbData($data_not_bid)
            ->upd();
        if(!$result_not_bid){
            IDb::dbRollback();
            self::$error = '确认失败';
            return false;
        }

        $data = [];
        $data['open_time'] = get_date_time();
        $data['tender_status'] = TenderStatus::Bid;
        $result = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($where_tender)
            ->setDbData($data)
            ->upd();
        if(!$result){
            IDb::dbRollback();
            self::$error = '确认失败';
            return false;
        }

        IDb::dbCommit();

        return true;
    }

    /**
     * 检查是否能投标
     * @param $agent_admin_id
     * @return bool|int
     */
    public static function canTender($agent_admin_id){
        $admin_info = IAgentAdminCache::getInfo($agent_admin_id);
        if(empty($admin_info['is_real']) || $admin_info['is_real'] != IsReal::yes){
            return -1;
        }

        return true;
    }

    /**
     * 投标
     * @param $agent_admin_id
     * @param $bid_id
     * @param $tender_note
     * @return bool
     */
    public static function tender($agent_admin_id,$bid_id,$tender_note =''){
        $can_tender = self::canTender($agent_admin_id);
        if($can_tender !== true){
            return $can_tender;
        }

        $where['user_pool_bid_id'] = $bid_id;
        $bid_info = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->row();
        if(empty($bid_info)){
            self::$error = "订单不存在！";
            return false;
        }

        if($bid_info['pool_bid_status'] != PoolBidStatus::Bid){
            self::$error = "该订单不可抢！";
            return false;
        }

        $result = IDb::getInstance('agent_admin_tender')
            ->setDbFiled('agent_admin_tender_id')
            ->setDbWhere(['user_pool_bid_id'=>$bid_id,'agent_admin_id'=>$agent_admin_id])
            ->row();
        if($result){
            self::$error = '不可重复抢单';
            return false;
        }

        $admin_info = IAgentAdminCache::getInfo($agent_admin_id,['agent_id']);
        if(empty($admin_info)){
            self::$error = "服务顾问信息错误！";
            return false;
        }

        $data['user_pool_bid_id'] = $bid_id;
        $data['agent_id'] = $admin_info['agent_id'];
        $data['agent_admin_id'] = $agent_admin_id;
        $data['tender_note'] = $tender_note;
        $data['tender_status'] = TenderStatus::NotOpen;
        $data['bond_money'] = 0; //TODO 投标保证金
        $data['bid_time'] = get_date_time();

        IDb::dbStartTrans();
        $result = IDb::getInstance('agent_admin_tender')
            ->setDbData($data)
            ->add();
        if(!$result){
            IDb::dbRollback();
            self::$error = '抢单失败';
            return false;
        }

        $bid_data['bid_num'] = ['exp','bid_num+1'];
        $result = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->setDbData($bid_data)
            ->upd();
        if(!$result){
            IDb::dbRollback();
            self::$error = '记录抢单失败';
            return false;
        }

        IDb::dbCommit();

        return $bid_info;
    }

    /**
     * 开标
     * @param $cooperate_admin_id
     * @param $bid_id
     * @return bool
     */
    public static function finish($cooperate_admin_id, $bid_id){

        $where['user_pool_bid_id'] = $bid_id;
        $where['cooperate_admin_id'] = $cooperate_admin_id;
        $bid_info = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->row();
        if(empty($bid_info)){
            self::$error = "订单不存在！";
            return false;
        }

        if($bid_info['pool_bid_status'] != PoolBidStatus::BidOpen){
            self::$error = "订单状态错误！";
            return false;
        }

        IDb::dbStartTrans();

        $where['user_pool_bid_id'] = $bid_id;

        $data['pool_bid_status'] = PoolBidStatus::BidStop;
        $data['stop_time'] = get_date_time();
        $result = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->setDbData($data)
            ->upd();
        if(!$result){
            IDb::dbRollback();
            self::$error = '确认失败';
            return false;
        }

        $data = [];
        $data['tender_status'] = TenderStatus::BidStop;
        $data['stop_time'] = date('Y-m-d H:i:s');
        $where_tender['agent_admin_id'] = $bid_info['agent_admin_id'];
        $where_tender['user_pool_bid_id'] = $bid_id;
        $result = IDb::getInstance('agent_admin_tender')
            ->setDbWhere($where_tender)
            ->setDbData($data)
            ->upd();
        if($result != 1){
            IDb::dbRollback();
            self::$error = '确认失败';
            return false;
        }

        IDb::dbCommit();

        return true;
    }

    /**
     * 评价
     * @param $cooperate_admin_id
     * @param $bid_id
     * @param $rate
     * @param $content
     * @param $is_anonymous
     * @return bool
     */
    public static function evaluate($cooperate_admin_id, $bid_id, $rate, $content, $is_anonymous){
        $where['user_pool_bid_id'] = $bid_id;
        $where['cooperate_admin_id'] = $cooperate_admin_id;
        $bid_info = IDb::getInstance('user_pool_bid')
            ->setDbWhere($where)
            ->row();
        if(empty($bid_info)){
            self::$error = "订单不存在！";
            return false;
        }

        if($bid_info['pool_bid_status'] != PoolBidStatus::BidStop){
            self::$error = "订单状态错误！";
            return false;
        }

        if($bid_info['is_evaluate'] != 1){
            self::$error = "订单已评价！";
            return false;
        }

        IDb::dbStartTrans();

        $rate = intval($rate);
        if($rate < 1 || $rate > 10){
            self::$error = '评分超出范围';
            return false;
        }


        $data['user_pool_bid_id'] = $bid_info['user_pool_bid_id'];
        $data['agent_admin_id'] = $bid_info['agent_admin_id'];
        $data['user_id'] = $bid_info['user_id'];
        $data['cooperate_admin_id'] = $cooperate_admin_id;
        $data['rate'] = $rate;
        $data['content'] = $content;
        $data['is_anonymous'] = $is_anonymous;
        $result = IDb::getInstance('agent_admin_evaluate')
            ->setDbData($data)
            ->add();
        if(!$result){
            IDb::dbRollback();
            self::$error = '添加评价失败';
            return false;
        }

        $result = IDb::getInstance('user_pool_bid')
            ->setDbWhere(['user_pool_bid_id'=>$bid_id])
            ->setDbData(['is_evaluate'=>2])
            ->upd();
        if(!$result){
            IDb::dbRollback();
            self::$error = '评价失败';
            return false;
        }

        $where_evaluate['agent_admin_id'] = $bid_info['agent_admin_id'];
        $count = IDb::getInstance('agent_admin_evaluate')
            ->setDbFiled('count(agent_admin_evaluate_id) as num,sum(rate) as total')
            ->setDbWhere($where_evaluate)
            ->setDbData($data)
            ->row();

        $count['num'] += 1;
        $count['total'] += $rate;

        $data = [];
        $data['evaluate_rate'] = round(bcdiv($count['total'],$count['num'],2));
        $where_admin['agent_admin_id'] = $bid_info['agent_admin_id'];
        $result = IDb::getInstance('agent_admin')
            ->setDbWhere($where_admin)
            ->setDbData($data)
            ->upd();
        if($result === false){
            IDb::dbRollback();
            self::$error = '确认失败';
            return false;
        }

        IDb::dbCommit();

        IAgentAdminCache::deleteInfo($bid_info['agent_admin_id']);
        IAgentAdminCache::deleteList();

        return true;
    }
}